package com.qianbao.qianbaobusiness.ui.help;

import com.androidbase.base.mvp.RxMvpPresenter;
import com.androidbase.utils.RxUtils;
import com.androidbase.utils.ToastUtils;
import com.baseandroid.config.Global;
import com.baseandroid.config.MyApi;
import com.baseandroid.config.UploadApi;
import com.baseandroid.retrofit.RxObserver;
import com.baseandroid.retrofit.json.Data;
import com.google.gson.Gson;
import com.qianbao.qianbaobusiness.json.FeedBackTagBean;
import com.qianbao.qianbaobusiness.json.UploadResult;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class FeedBackPresenter extends RxMvpPresenter<FeedBackView> {
    public static final String MULTIPART_FORM_DATA = "multipart/form-data";
    MyApi     myApi;
    UploadApi mUploadApi;
    int requestCount = 0;

    @Inject
    public FeedBackPresenter(MyApi myApi, UploadApi uploadApi) {
        this.myApi = myApi;
        mUploadApi = uploadApi;
    }

    public void getFeedbackTags(String sceneId) {
        myApi.getFeedbackTags(sceneId).compose(RxUtils.applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data<FeedBackTagBean>>() {
                    @Override
                    public void onNext(Data<FeedBackTagBean> data) {
                        super.onNext(data);
                        if (RxObserver.checkJsonCode(data, false)) {
                            getView().getFeedbackTagSuccess(data.getResult());
                        } else {
                            if (requestCount++ < 5) {
                                getFeedbackTags(sceneId);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (requestCount++ < 5) {
                            getFeedbackTags(sceneId);
                        }
                    }
                });
    }


    public void uploadImg(File file) {

        MultipartBody.Part body = prepareFilePart("file", file);
        HashMap<String, RequestBody> params = new HashMap<>();
        getView().showLoadingDialog();
        mUploadApi.uploadFileWithPartMap(params, body).compose(RxUtils.applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<ResponseBody>() {
                    @Override
                    public void onComplete() {
                        super.onComplete();
                        getView().closeLoadingDialog();
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        super.onNext(responseBody);
                        try {
                            String repone = responseBody.string();
                            Gson gson = new Gson();
                            UploadResult uploadResult = gson.fromJson(repone, UploadResult.class);
                            UploadResult.UploadFileBean uploadFile = uploadResult.getUploadFile();
                            getView().uploadImgSuccess(uploadFile.getUrl(), uploadFile.getName());
                        } catch (IOException e) {
                            e.printStackTrace();
                            getView().closeLoadingDialog();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        ToastUtils.showLongToast("图片上传失败");
                        getView().closeLoadingDialog();
                    }
                });
    }

    private static MultipartBody.Part prepareFilePart(String partName, File file) {
        // create RequestBody instance from file
        RequestBody requestFile = RequestBody.create(MediaType.parse(MULTIPART_FORM_DATA), file);
        // MultipartBody.Part is used to send also the actual file name
        return MultipartBody.Part.createFormData(partName, file.getName(), requestFile);
    }

    public void submit(String content, String pageUrl, String sceneId, String tagList, String attFlList, String _userNo) {
        Map<String, String> params = new HashMap<>();
        params.put("content", content);
        params.put("merchantno", Global.getShopListInfo().merchantList.size() > 0 ? Global.getShopListInfo().merchantList.get(0).merchantNo : "");
        params.put("pageUrl", pageUrl);
        params.put("sceneId", sceneId);
        params.put("tagList", tagList);
        params.put("attFlList", attFlList);
        params.put("_userNo", _userNo);
        myApi.submitFeedback(params).compose(RxUtils.applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data>() {
                    @Override
                    public void onNext(Data data) {
                        super.onNext(data);
                        if (RxObserver.checkJsonCode(data, true)) {
                            getView().submitSuccess();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }
}
